package com.example.tijianbackend.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/25 15:14
 * @version: 1.0
 */

public class SmsUtil {


    public static void sendMessage(String code) throws ExecutionException,InterruptedException{



        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tH4UdoncjfggES3hViP")
                .accessKeySecret("jIskKDuBDJTwZGlqt4lAW0xhldiwAu")
                .build());


        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") //
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        // Parameter settings for API request
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("smsService")
                .templateCode("SMS_468720030")
                .phoneNumbers("15767063974")
                .templateParam(String.format("{\"code\":\"%s\"}",code))
                .build();


        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();
    }


}
